<template>
    <div style="position: relative;height: 100%;">
        <a-spin size="large" tip="Loading..." style="position: absolute;top: 30%;left: 50%;"/>
    </div>
</template>

<script>
    import { grant,getMyInfo } from "../../apis";
    export default {
        props : {
            code : { type : String, required : true },
            flag : { type : String, required : true },
            callback : { type : String, required : true }
        },
        mounted() {
            let $this = this;
            //  账号认证
            grant(this.code,this.flag)
                .then(res => {
                    //  保存访问令牌
                    $this.$store.commit('setAccessToken',res.data);
                    //  回到登陆前要访问的页面
                    $this.$router.push({ path : $this.callback }).then();
                    //  获取开发者信息
                    getMyInfo().then(result => {
                        $this.$store.commit('setUserInfo',result.data);
                        if ($this.$store.getters.isNewUser){
                            $this.$router.push({ name : 'register',query : { callback : encodeURI($this.$route.fullPath) } }).then();
                        }
                    });
                }).catch(e => {
                    //  重新获取授权码
                    $this.$modal.confirm({
                        title : e.code,
                        content : e.msg,
                        onOk : () =>
                            window.location.href = $this.$store.getters.getOauthAuthorizeUrl + '&callback=' + encodeURI($this.callback),
                        onCancel : () => {
                            //  摧毁全部弹窗
                            $this.$modal.destroyAll();
                            //  回到首页
                            $this.$router.push({name: 'index'}).then();
                        }
                    });
                });

        }
    }
</script>
